var express = require('express');
var app = express();
var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);
var schedule = require('node-schedule');
var mysql = require('./dao/mysql/mysql');
var cronTask = require('./core/cronTask');
var user = require('./core/user');
var disUser = require('./core/disUser');
var turntable = require('./core/turntable');
var qrImage = require('./app/qrmode/qrImage');
var wxtool = require('./app/wechat/wxtool');
var disWxTool = require('./app/wechat/disWxTool');
var wxPay = require('./app/wechat/wxPay');


app.configure(function(){
  app.use(express.methodOverride());
  app.use(express.urlencoded());
  app.use(bodyParser.xml({
      limit: '1MB', // Reject payload bigger than 1 MB
      xmlParseOptions: { normalize: true, // Trim whitespace inside text nodes
      normalizeTags: true, // Transform tags to lowercase
      explicitArray: false // Only put nodes in array if >1
    }
  }));

  app.use(express.json());
  app.use(app.router);
  app.set('view engine', 'jade');
  app.set('views', __dirname + '/public');
  app.set('view options', {layout: false});
  app.set('basepath',__dirname + '/public');
});

app.configure('development', function(){
  app.use(express.static(__dirname + '/public'));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  var oneYear = 31557600000;
  app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
  app.use(express.errorHandler());
});

mysql.init();

app.all('*',function (req,res,next) {
  var ipUrls = ['/pay','/sendPacket','/transfers','/disPay','/disTransfers']
  if(ipUrls.indexOf(req.url) != -1){
    req.ipAddress = (req.connection.remoteAddress || req.socket.remoteAddress).replace('::ffff:','');
    //console.log('1:%s  2:%s',req.connection.remoteAddress,req.socket.remoteAddress);
  }
  next();
});

/******** WX OATH ************/

app.get('/wxInit', wxtool.wxInit);

/******** turnPlate ************/

app.get('/wxCode', wxtool.wxCode);
app.get('/wxOpenid', wxtool.wxOpenid);
app.get('/userInfo', user.getUserInfo);
app.get('/turntable/rotateItem',turntable.rotateItem);
app.get('/createQR',qrImage.createQR);
app.get('/yzpng',user.yzpng);

app.get('/pay', wxPay.pay);
app.get('/sendPacket', wxPay.sendPacket);
app.get('/transfers', wxPay.transfers);
app.post('/wxPaySign', wxPay.wxPaySign);

app.get('/getReferee1Info',user.getReferee1Info);
app.get('/getReferee2Info',user.getReferee2Info);
app.get('/getRefereeNum',user.getRefereeNum);
app.get('/getPostList',user.getPostList);
app.get('/extractCoin',user.extractCoin);
app.get('/getCashList',user.getCashList);

app.get('/getAllUser',user.getAllUser);
app.get('/getUserOrders',user.getUserOrders);
app.get('/getPostCash',user.getPostCash);

app.get('/getChannelWorth',user.getChannelWorth);
app.get('/balanceChannel',user.balanceChannel);
app.post('/listChannel',user.listChannel);
app.get('/listHistory',user.listHistory);
app.get('/getChannelUser',user.getChannelUser);
app.get('/getChannelOrders',user.getChannelOrders);
app.get('/getChannelCash',user.getChannelCash);


/******** distribution ************/

app.get('/disWxCode', disWxTool.disWxCode);
app.get('/disWxOpenid', disWxTool.disWxOpenid);
app.get('/disUserInfo', disUser.getUserInfo);
app.get('/disUidInfo', disUser.getUidInfo);
app.get('/createDisQR',qrImage.createDisQR);

app.get('/disPay', wxPay.disPay);
app.get('/disMaPay', wxPay.disMaPay);
app.post('/disWxPaySign', wxPay.disWxPaySign);
app.post('/disMaPaySign', wxPay.disMaPaySign);
app.get('/disTransfers', wxPay.disTransfers);
app.get('/disPoinPay',disUser.disPoinPay);

app.get('/getComiList',disUser.getComiList);
app.get('/getPoinList',disUser.getPoinList);
app.get('/getDisextractCoin',disUser.extractCoin);
app.get('/getDisCashList',disUser.getCashList);
app.post('/sendPoin',disUser.sendPoin);

app.get('/getDisRefereeNum',disUser.getRefereeNum);
app.get('/getDisRefereeInfo',disUser.getDisRefereeInfo);

app.get('/getDisUser',disUser.getDisUser);
app.get('/getDisOrders',disUser.getDisOrders);
app.get('/getDisCash',disUser.getDisCash);

app.get('/getDisChannelWorth',disUser.getDisChannelWorth);
app.get('/balanceDisChannel',disUser.balanceDisChannel);
app.post('/listDisChannel',disUser.listDisChannel);
app.get('/listDisHistory',disUser.listDisHistory);
app.get('/getDisChannelUser',disUser.getDisChannelUser);
app.get('/getDisChannelOrders',disUser.getDisChannelOrders);
app.get('/getDisChannelCash',disUser.getDisChannelCash);

schedule.scheduleJob('0 0 0 * * *',cronTask.recorderEveryData);   // second /minute /hour *通配符
//schedule.scheduleJob('0 0 0 * * 0', cronTask.recordPlayerWeekCoin); // day of month/ month /day of week

//disUser.createCash();
//disUser.createCom();
//disUser.createPoin();

console.log("Web server has started.\nPlease log on weblogin.178dn.net/index.html");

app.listen(80);

process.on('uncaughtException', function (err) {
  console.error("Caught exception:", err.stack);
});
process.on('unhandledRejection', function (reason, p) {
  console.error("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack);
});